/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { Buffer } from './src/main/core/buffer/index.js'

function alloc_Test_01() {
  const source = 1024;
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 10000; i++) {
    result = Buffer.alloc(source)
  }
  let endTime = Date.now();
  if (result.length != 1024) {
    throw 'Buffer.alloc error'
  }
  print(`node-polyfill_alloc(size): ${endTime - startTime} ms`)
}

function alloc_Test_02() {
  const source = 1024;
  const content = 'buffer'
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 10000; i++) {
    result = Buffer.alloc(source, content)
  }
  let endTime = Date.now();
  if (!result.toString().includes(content)) {
    throw 'Buffer.alloc error'
  }
  print(`node-polyfill_alloc(string): ${endTime - startTime} ms`)
}

function alloc_Test_03() {
  const source = 1024;
  const content = 'aGVsbG8gd29ybGQ='
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 10000; i++) {
    result = Buffer.alloc(source, content, 'base64')
  }
  let endTime = Date.now();
  if (!result.toString('').includes('hello')) {
    throw 'Buffer.alloc error'
  }
  print(`node-polyfill_alloc(base64): ${endTime - startTime} ms`)
}

function alloc_Test_04() {
  const source = 1024;
  const content = '74686973206973206120627566666572';
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 10000; i++) {
    result = Buffer.alloc(source, content, 'hex')
  }
  let endTime = Date.now();
  if (!result.toString('hex').includes(content)) {
    throw 'Buffer.alloc error'
  }
  print(`node-polyfill_alloc(hex): ${endTime - startTime} ms`)
}

function alloc_Test_05() {
  const source = 1024;
  const content = '这是一个长度为1024的buffer';
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 10000; i++) {
    result = Buffer.alloc(source, content, 'utf8')
  }
  let endTime = Date.now();
  if (!result.toString('utf8').includes(content)) {
    throw 'Buffer.alloc error'
  }
  print(`node-polyfill_alloc(utf8): ${endTime - startTime} ms`)
}

function alloc_Test_06() {
  const source = 1024;
  const content = 'this is a Buffer'
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 10000; i++) {
    result = Buffer.alloc(source, content, 'ascii')
  }
  let endTime = Date.now();
  if (!result.toString('ascii').includes(content)) {
    throw 'Buffer.alloc(ascii) error'
  }
  print(`node-polyfill_alloc(ascii): ${endTime - startTime} ms`)
}

function allocUnsafe_Test() {
  const source = 256;
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 50000; i++) {
    result = Buffer.allocUnsafe(source)
  }
  let endTime = Date.now();
  if (!Buffer.isBuffer(result)) {
    throw 'Buffer.allocUnsafe error'
  }
  print(`node-polyfill_allocUnsafe: ${endTime - startTime} ms`)
}

function allocUnsafeSlow_Test() {
  const source = 256;
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 50000; i++) {
    result = Buffer.allocUnsafeSlow(source)
  }
  let endTime = Date.now();
  if (!Buffer.isBuffer(result)) {
    throw 'Buffer.allocUnsafeSlow error'
  }
  print(`node-polyfill_allocUnsafeSlow: ${endTime - startTime} ms`)
}

function byteLength_Test() {
  const source = '\u00bd + \u00bc = \u00be';
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 50000; i++) {
    result = Buffer.byteLength(source, 'utf8')
  }
  let endTime = Date.now();
  if (result != 12) {
    throw 'Buffer.byteLength error'
  }
  print(`node-polyfill_byteLength: ${endTime - startTime} ms`)
}

function concat_Test() {
  const buf1 = Buffer.alloc(10);
  const buf2 = Buffer.alloc(14);
  const buf3 = Buffer.alloc(18);
  const totalLength = buf1.length + buf2.length + buf3.length;
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    result = Buffer.concat([buf1, buf2, buf3]);
  }
  let endTime = Date.now();
  if (result.length != totalLength) {
    throw 'Buffer.concat error'
  }
  print(`node-polyfill_concat: ${endTime - startTime} ms`)
}

function from_Test_01() {
  const source = [0x62, 0x75, 0x66, 0x66, 0x65, 0x72];
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    result = Buffer.from(source)
  }
  let endTime = Date.now();
  if (!Buffer.isBuffer(result)) {
    throw 'Buffer.from01 error'
  }
  print(`node-polyfill_from(Array): ${endTime - startTime} ms`)
}

function from_Test_02() {
  const arr = new Uint16Array(2);
  arr[0] = 5000;
  arr[1] = 4000;
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    result = Buffer.from(arr.buffer)
  }
  let endTime = Date.now();
  if (!Buffer.isBuffer(result)) {
    throw 'Buffer.from02 error'
  }
  print(`node-polyfill_from(ArrayBuffer): ${endTime - startTime} ms`)
}

function from_Test_03() {
  const buf1 = Buffer.from('buffer');
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 10000; i++) {
    result = Buffer.from(buf1)
  }
  let endTime = Date.now();
  if (!Buffer.isBuffer(result)) {
    throw 'Buffer.from03 error'
  }
  print(`node-polyfill_from(buffer): ${endTime - startTime} ms`)
}

function from_Test_04() {
  const source = new String('this is a test');
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    result = Buffer.from(source)
  }
  let endTime = Date.now();
  if (!Buffer.isBuffer(result)) {
    throw 'Buffer.from04 error'
  }
  print(`node-polyfill_from(Object): ${endTime - startTime} ms`)
}


function from_Test_05() {
  class Foo {
    [Symbol.toPrimitive]() {
      return 'this is a test';
    }
  }
  const source = new Foo();
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    result = Buffer.from(source)
  }
  let endTime = Date.now();
  if (!Buffer.isBuffer(result)) {
    throw 'Buffer.from05 error'
  }
  print(`node-polyfill_from(SymbolObject): ${endTime - startTime} ms`)
}

function from_Test_06() {
  const source = 'this is a tést';
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    result = Buffer.from('7468697320697320612074c3a97374', 'hex')
  }
  let endTime = Date.now();
  if (result.toString() != source) {
    throw 'Buffer.from06 error'
  }
  print(`node-polyfill_from(string): ${endTime - startTime} ms`)
}

function isBuffer_Test() {
  const buf1 = 'this is a tést';
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    result = Buffer.isBuffer(buf1)
  }
  let endTime = Date.now();
  if (result) {
    throw 'Buffer.isBuffer error'
  }
  print(`node-polyfill_isBuffer: ${endTime - startTime} ms`)
}

function isEncoding_Test() {
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    result = Buffer.isEncoding('utf8')
  }
  let endTime = Date.now();
  if (!result) {
    throw 'Buffer.isEncoding error'
  }
  print(`node-polyfill_isEncoding: ${endTime - startTime} ms`)
}

function compare_Test() {
  const buf1 = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 9]);
  const buf2 = Buffer.from([5, 6, 7, 8, 9, 1, 2, 3, 4]);
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 10000; i++) {
    result = buf1.compare(buf2, 5, 9, 0, 4)
  }
  let endTime = Date.now();
  if (result != 0) {
    throw 'buf.compare error'
  }
  print(`node-polyfill_compare: ${endTime - startTime} ms`)
}

function copy_Test() {
  const buf1 = Buffer.allocUnsafe(26);
  const buf2 = Buffer.allocUnsafe(26).fill('!');
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    buf1.copy(buf2, 8, 16, 20);
  }
  let endTime = Date.now();
  if (!buf2.includes(buf1.subarray(16, 20))) {
    throw 'buf.copy error'
  }
  print(`node-polyfill_copy: ${endTime - startTime} ms`)
}

function entries_Test() {
  const buf = Buffer.from('buffer');
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    result = buf.entries();
  }
  let endTime = Date.now();
  for (const pair of result) {
    if (pair[0] > 5) {
      throw 'buf.entries error'
    }
  }
  print(`node-polyfill_entries: ${endTime - startTime} ms`)
}

function equals_Test() {
  const buf1 = Buffer.from('ABC');
  const buf2 = Buffer.from('414243', 'hex');
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 10000; i++) {
    result = buf1.equals(buf2);
  }
  let endTime = Date.now();
  if (!result) {
    throw 'buf.equals error'
  }
  print(`node-polyfill_equals: ${endTime - startTime} ms`)
}

function fill_Test_01() {
  const buf1 = Buffer.allocUnsafe(50)
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    result = buf1.fill('help');
  }
  let endTime = Date.now();
  if (!result.includes('help')) {
    throw 'buf.fill error'
  }
  print(`node-polyfill_fill: ${endTime - startTime} ms`)
}

function fill_Test_02() {
  const buf1 = Buffer.allocUnsafe(50)
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    result = buf1.fill('help', 5, 45);
  }
  let endTime = Date.now();
  if (!result.includes('help')) {
    throw 'buf.fill error'
  }
  print(`node-polyfill_fill(offset): ${endTime - startTime} ms`)
}

function fill_Test_03() {
  const buf1 = Buffer.allocUnsafe(50)
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    result = buf1.fill('help', 'base64');
  }
  let endTime = Date.now();
  if (!result.toString('base64').includes('help')) {
    throw 'buf.fill error'
  }
  print(`node-polyfill_fill(encoding): ${endTime - startTime} ms`)
}

function includes_Test_01() {
  const buf = Buffer.from('this is a buffer');
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    result = buf.includes('this');
  }
  let endTime = Date.now();
  if (!result) {
    throw 'buf.includes error'
  }
  print(`node-polyfill_includes: ${endTime - startTime} ms`)
}

function includes_Test_02() {
  const buf = Buffer.from('this is a buffer');
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    result = buf.includes('this', 4);
  }
  let endTime = Date.now();
  if (result) {
    throw 'buf.includes error'
  }
  print(`node-polyfill_includes(offset): ${endTime - startTime} ms`)
}

function includes_Test_03() {

  const buf = Buffer.from('this is a buffer');
  const searchStr = Buffer.from('YnVmZmVy', 'base64')
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    result = buf.includes(searchStr, 2, 'base64');
  }
  let endTime = Date.now();
  if (!result) {
    throw 'buf.includes error'
  }
  print(`node-polyfill_includes(encoding): ${endTime - startTime} ms`)
}

function indexOf_Test_01() {
  const buf = Buffer.from('this is a buffer');
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    result = buf.indexOf('is');
  }
  let endTime = Date.now();
  if (result != 2) {
    throw 'buf.indexOf error'
  }
  print(`node-polyfill_indexOf: ${endTime - startTime} ms`)
}

function indexOf_Test_02() {
  const buf = Buffer.from('this is a buffer');
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    result = buf.indexOf('a', 7);
  }
  let endTime = Date.now();
  if (result != 8) {
    throw 'buf.indexOf error'
  }
  print(`node-polyfill_indexOf(offset): ${endTime - startTime} ms`)
}

function indexOf_Test_03() {
  const buf = Buffer.from('this is a buffer');
  const searchStr = Buffer.from('YnVmZmVy', 'base64')
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    result = buf.indexOf(searchStr, 5, 'base64');
  }
  let endTime = Date.now();
  if (result != 10) {
    throw 'buf.indexOf error'
  }
  print(`node-polyfill_indexOf(encoding): ${endTime - startTime} ms`)
}

function lastIndexOf_Test_01() {
  const buf = Buffer.from('this buffer is a buffer');
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    result = buf.lastIndexOf('buffer');
  }
  let endTime = Date.now();
  if (result != 17) {
    throw 'buf.lastIndexOf error'
  }
  print(`node-polyfill_lastIndexOf: ${endTime - startTime} ms`)
}

function lastIndexOf_Test_02() {
  const buf = Buffer.from('this buffer is a buffer');
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    result = buf.lastIndexOf('buffer', 5);
  }
  let endTime = Date.now();
  if (result != 5) {
    throw 'buf.lastIndexOf error'
  }
  print(`node-polyfill_lastIndexOf(offset): ${endTime - startTime} ms`)
}

function lastIndexOf_Test_03() {
  const buf = Buffer.from('this buffer is a buffer');
  const searchStr = Buffer.from('YnVmZmVy', 'base64')
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    result = buf.lastIndexOf(searchStr, 5, 'base64');
  }
  let endTime = Date.now();
  if (result != 5) {
    throw 'buf.lastIndexOf error'
  }
  print(`node-polyfill_lastIndexOf(encoding): ${endTime - startTime} ms`)
}

function subarray_Test() {
  const buf = Buffer.from('buffer');
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    result = buf.subarray(0, 5)
  }
  let endTime = Date.now();
  if (result.toString() != 'buffe') {
    throw 'buf.subarray error'
  }
  print(`node-polyfill_subarray: ${endTime - startTime} ms`)
}

function swap16_Test() {
  const buf1 = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]);
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    buf1.swap16();
  }
  let endTime = Date.now();
  if (buf1[0] != 1) {
    throw 'buf.swap16 error'
  }
  print(`node-polyfill_swap16: ${endTime - startTime} ms`)
}

function swap32_Test() {
  const buf1 = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]);
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    buf1.swap32();
  }
  let endTime = Date.now();
  if (buf1[0] != 1) {
    throw 'buf.swap32 error'
  }
  print(`node-polyfill_swap32: ${endTime - startTime} ms`)
}

function swap64_Test() {
  const buf1 = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]);
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    buf1.swap64();
  }
  let endTime = Date.now();
  if (buf1[0] != 1) {
    throw 'buf.swap64 error'
  }
  print(`node-polyfill_swap64: ${endTime - startTime} ms`)
}

function toString_Test_01() {
  const buf1 = Buffer.allocUnsafe(26);
  for (let i = 0; i < 26; i++) {
    // 97 是 'a' 的十进制 ASCII 值。
    buf1[i] = i + 97;
  }
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    result = buf1.toString('utf8');
  }
  let endTime = Date.now();
  if (result != 'abcdefghijklmnopqrstuvwxyz') {
    throw 'buf.toString01 error'
  }
  print(`node-polyfill_toString(utf8): ${endTime - startTime} ms`)
}

function toString_Test_02() {
  const buf1 = Buffer.from('The quick brown fox jumps over the lazy dog.');
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    result = buf1.toString('hex');
  }
  let endTime = Date.now();
  if (result != '54686520717569636b2062726f776e20666f78206a756d7073206f76657220746865206c617a7920646f672e') {
    throw 'buf.toString02 error'
  }
  print(`node-polyfill_toString(hex): ${endTime - startTime} ms`)
}

function toString_Test_03() {
  const buf1 = Buffer.from('The quick brown fox jumps over the lazy dog.');
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    result = buf1.toString('base64');
  }
  let endTime = Date.now();
  if (result != 'VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZy4=') {
    throw 'buf.toString03 error'
  }
  print(`node-polyfill_toString(base64): ${endTime - startTime} ms`)
}

function keys_Test() {
  const buf = Buffer.from('buffer');
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    result = buf.keys();
  }
  let endTime = Date.now();
  for (const pair of result) {
    if (pair == null) {
      throw 'buf.keys error'
    }
  }
  print(`node-polyfill_keys: ${endTime - startTime} ms`)
}

function values_Test() {
  const buf = Buffer.from('buffer');
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    result = buf.values();
  }
  let endTime = Date.now();
  for (const pair of result) {
    if (pair == null) {
      throw 'buf.values error'
    }
  }
  print(`node-polyfill_values: ${endTime - startTime} ms`)
}

function readBigInt64BE_Test() {
  const buf = Buffer.from([0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff]);
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    result = buf.readBigInt64BE(0);
  }
  let endTime = Date.now();
  if (result != 4294967295n) {
    throw 'buf.readBigInt64BE error'
  }
  print(`node-polyfill_readBigInt64BE: ${endTime - startTime} ms`)
}


function readBigInt64LE_Test() {
  const buf = Buffer.from([0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff]);
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    result = buf.readBigInt64LE(0);
  }
  let endTime = Date.now();
  if (result != -4294967296n) {
    throw 'buf.readBigInt64LE error'
  }
  print(`node-polyfill_readBigInt64LE: ${endTime - startTime} ms`)
}


function readBigUInt64BE_Test() {
  const buf = Buffer.from([0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff]);
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    result = buf.readBigUInt64BE(0);
  }
  let endTime = Date.now();
  if (result != 4294967295n) {
    throw 'buf.readBigUInt64BE error'
  }
  print(`node-polyfill_readBigUInt64BE: ${endTime - startTime} ms`)
}


function readBigUInt64LE_Test() {
  const buf = Buffer.from([0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff]);
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    result = buf.readBigUInt64LE(0);
  }
  let endTime = Date.now();
  if (result != 18446744069414584320n) {
    throw 'buf.readBigUInt64LE error'
  }
  print(`node-polyfill_readBigUInt64LE: ${endTime - startTime} ms`)
}

function readDoubleBE_Test() {
  const buf = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]);
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    result = buf.readDoubleBE(0);
  }
  let endTime = Date.now();
  if (result != 8.20788039913184e-304) {
    throw 'buf.readDoubleBE error'
  }
  print(`node-polyfill_readDoubleBE: ${endTime - startTime} ms`)
}


function readDoubleLE_Test() {
  const buf = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]);
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    result = buf.readDoubleLE(0);
  }
  let endTime = Date.now();
  if (result != 5.447603722011605e-270) {
    throw 'buf.readDoubleLE error'
  }
  print(`node-polyfill_readDoubleLE: ${endTime - startTime} ms`)
}

function readFloatBE_Test() {
  const buf = Buffer.from([1, 2, 3, 4]);
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    result = buf.readFloatBE(0);
  }
  let endTime = Date.now();
  if (result != 2.387939260590663e-38) {
    throw 'buf.readFloatBE error'
  }
  print(`node-polyfill_readFloatBE: ${endTime - startTime} ms`)
}


function readFloatLE_Test() {
  const buf = Buffer.from([1, 2, 3, 4]);
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    result = buf.readFloatLE(0);
  }
  let endTime = Date.now();
  if (result != 1.539989614439558e-36) {
    throw 'buf.readFloatLE error'
  }
  print(`node-polyfill_readFloatLE: ${endTime - startTime} ms`)
}

function readInt8_Test() {
  const buf = Buffer.from([-1, 5]);
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    result = buf.readInt8(0);
  }
  let endTime = Date.now();
  if (result != -1) {
    throw 'buf.readInt8 error'
  }
  print(`node-polyfill_readInt8: ${endTime - startTime} ms`)
}

function readInt16BE_Test() {
  const buf = Buffer.from([0, 5]);
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    result = buf.readInt16BE(0);
  }
  let endTime = Date.now();
  if (result != 5) {
    throw 'buf.readInt16BE error'
  }
  print(`node-polyfill_readInt16BE: ${endTime - startTime} ms`)
}


function readInt16LE_Test() {
  const buf = Buffer.from([0, 5]);
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    result = buf.readInt16LE(0);
  }
  let endTime = Date.now();
  if (result != 1280) {
    throw 'buf.readInt16LE error'
  }
  print(`node-polyfill_readInt16LE: ${endTime - startTime} ms`)
}

function readInt32BE_Test() {
  const buf = Buffer.from([0, 0, 0, 5]);
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    result = buf.readInt32BE(0);
  }
  let endTime = Date.now();
  if (result != 5) {
    throw 'buf.readInt32BE error'
  }
  print(`node-polyfill_readInt32BE: ${endTime - startTime} ms`)
}


function readInt32LE_Test() {
  const buf = Buffer.from([0, 0, 0, 5]);
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    result = buf.readInt32LE(0);
  }
  let endTime = Date.now();
  if (result != 83886080) {
    throw 'buf.readInt32LE error'
  }
  print(`node-polyfill_readInt32LE: ${endTime - startTime} ms`)
}

function readIntBE_Test() {
  const buf = Buffer.from([0x12, 0x34, 0x56, 0x78, 0x90, 0xab]);
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    result = buf.readIntBE(0, 6);
  }
  let endTime = Date.now();
  if (result.toString(16) != '1234567890ab') {
    throw 'buf.readIntBE error'
  }
  print(`node-polyfill_readIntBE: ${endTime - startTime} ms`)
}


function readIntLE_Test() {
  const buf = Buffer.from([0x12, 0x34, 0x56, 0x78, 0x90, 0xab]);
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    result = buf.readIntLE(0, 6);
  }
  let endTime = Date.now();
  if (result.toString(16) != '-546f87a9cbee') {
    throw 'buf.readIntLE error'
  }
  print(`node-polyfill_readIntLE: ${endTime - startTime} ms`)
}

function readUInt8_Test() {
  const buf = Buffer.from([1, -2]);
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    result = buf.readUInt8(0);
  }
  let endTime = Date.now();
  if (result != 1) {
    throw 'buf.readUInt8 error'
  }
  print(`node-polyfill_readUInt8: ${endTime - startTime} ms`)
}

function readUInt16BE_Test() {
  const buf = Buffer.from([0x12, 0x34, 0x56]);
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    result = buf.readUInt16BE(0);
  }
  let endTime = Date.now();
  if (result.toString(16) != '1234') {
    throw 'buf.readUInt16BE error'
  }
  print(`node-polyfill_readUInt16BE: ${endTime - startTime} ms`)
}


function readUInt16LE_Test() {
  const buf = Buffer.from([0x12, 0x34, 0x56]);
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    result = buf.readUInt16LE(0);
  }
  let endTime = Date.now();
  if (result.toString(16) != '3412') {
    throw 'buf.readUInt16LE error'
  }
  print(`node-polyfill_readUInt16LE: ${endTime - startTime} ms`)
}

function readUInt32BE_Test() {
  const buf = Buffer.from([0x12, 0x34, 0x56, 0x78]);
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    result = buf.readUInt32BE(0);
  }
  let endTime = Date.now();
  if (result.toString(16) != '12345678') {
    throw 'buf.readUInt32BE error'
  }
  print(`node-polyfill_readUInt32BE: ${endTime - startTime} ms`)
}


function readUInt32LE_Test() {
  const buf = Buffer.from([0x12, 0x34, 0x56, 0x78]);
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    result = buf.readUInt32LE(0);
  }
  let endTime = Date.now();
  if (result.toString(16) != '78563412') {
    throw 'buf.readUInt32LE error'
  }
  print(`node-polyfill_readUInt32LE: ${endTime - startTime} ms`)
}

function readUIntBE_Test() {
  const buf = Buffer.from([0x12, 0x34, 0x56, 0x78, 0x90, 0xab]);
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    result = buf.readUIntBE(0, 6);
  }
  let endTime = Date.now();
  if (result.toString(16) != '1234567890ab') {
    throw 'buf.readUIntBE error'
  }
  print(`node-polyfill_readUIntBE: ${endTime - startTime} ms`)
}


function readUIntLE_Test() {
  const buf = Buffer.from([0x12, 0x34, 0x56, 0x78, 0x90, 0xab]);
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    result = buf.readUIntLE(0, 6);
  }
  let endTime = Date.now();
  if (result.toString(16) != 'ab9078563412') {
    throw 'buf.readUIntLE error'
  }
  print(`node-polyfill_readUIntLE: ${endTime - startTime} ms`)
}


function write_Test() {
  const buf = Buffer.alloc(256);
  let result = null;
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    result = buf.write(`\u00bd + \u00bc = \u00be`, 0);
  }
  let endTime = Date.now();
  if (result != 12) {
    throw 'buf.write error'
  }
  print(`node-polyfill_write: ${endTime - startTime} ms`)
}

function writeBigInt64BE_Test() {
  const buf = Buffer.allocUnsafe(8);
  let startTime = Date.now();
  for (let i = 0; i < 100000; i++) {
    buf.writeBigInt64BE(0x0102030405060708n, 0);
  }
  let endTime = Date.now();
  if (buf.length != 8) {
    throw 'buf.writeBigInt64BE error'
  }
  print(`node-polyfill_writeBigInt64BE: ${endTime - startTime} ms`)
}

function writeBigInt64LE_Test() {
  const buf = Buffer.allocUnsafe(8);
  let startTime = Date.now();
  for (let i = 0; i < 100000; i++) {
    buf.writeBigInt64LE(0x0102030405060708n, 0);
  }
  let endTime = Date.now();
  if (buf.length != 8) {
    throw 'buf.writeBigInt64LE error'
  }
  print(`node-polyfill_writeBigInt64LE: ${endTime - startTime} ms`)
}

function writeBigUInt64BE_Test() {
  const buf = Buffer.allocUnsafe(8);
  let startTime = Date.now();
  for (let i = 0; i < 100000; i++) {
    buf.writeBigUInt64BE(0xdecafafecacefaden, 0);
  }
  let endTime = Date.now();
  if (buf.length != 8) {
    throw 'buf.writeBigUInt64BE error'
  }
  print(`node-polyfill_writeBigUInt64BE: ${endTime - startTime} ms`)
}

function writeBigUInt64LE_Test() {
  const buf = Buffer.allocUnsafe(8);
  let startTime = Date.now();
  for (let i = 0; i < 100000; i++) {
    buf.writeBigUInt64LE(0xdecafafecacefaden, 0);
  }
  let endTime = Date.now();
  if (buf.length != 8) {
    throw 'buf.writeBigUInt64LE error'
  }
  print(`node-polyfill_writeBigUInt64LE: ${endTime - startTime} ms`)
}

function writeDoubleBE_Test() {
  const buf = Buffer.allocUnsafe(8);
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    buf.writeDoubleBE(123.456, 0);
  }
  let endTime = Date.now();
  if (buf.length != 8) {
    throw 'buf.writeDoubleBE error'
  }
  print(`node-polyfill_writeDoubleBE: ${endTime - startTime} ms`)
}

function writeDoubleLE_Test() {
  const buf = Buffer.allocUnsafe(8);
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    buf.writeDoubleLE(123.456, 0);
  }
  let endTime = Date.now();
  if (buf.length != 8) {
    throw 'buf.writeDoubleLE error'
  }
  print(`node-polyfill_writeDoubleLE: ${endTime - startTime} ms`)
}

function writeFloatBE_Test() {
  const buf = Buffer.allocUnsafe(4);
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    buf.writeFloatBE(0xcafebabe, 0);
  }
  let endTime = Date.now();
  if (buf.length != 4) {
    throw 'buf.writeFloatBE error'
  }
  print(`node-polyfill_writeFloatBE: ${endTime - startTime} ms`)
}

function writeFloatLE_Test() {
  const buf = Buffer.allocUnsafe(4);
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    buf.writeFloatLE(0xcafebabe, 0);
  }
  let endTime = Date.now();
  if (buf.length != 4) {
    throw 'buf.writeFloatLE error'
  }
  print(`node-polyfill_writeFloatLE: ${endTime - startTime} ms`)
}

function writeInt8_Test() {
  const buf = Buffer.allocUnsafe(2);
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    buf.writeInt8(2, 0);
  }
  let endTime = Date.now();
  if (buf.length != 2) {
    throw 'buf.writeInt8 error'
  }
  print(`node-polyfill_writeInt8: ${endTime - startTime} ms`)
}

function writeInt16BE_Test() {
  const buf = Buffer.allocUnsafe(2);
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    buf.writeInt16BE(0x0102, 0);
  }
  let endTime = Date.now();
  if (buf.length != 2) {
    throw 'buf.writeInt16BE error'
  }
  print(`node-polyfill_writeInt16BE: ${endTime - startTime} ms`)
}

function writeInt16LE_Test() {
  const buf = Buffer.allocUnsafe(2);
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    buf.writeInt16LE(0x0304, 0);
  }
  let endTime = Date.now();
  if (buf.length != 2) {
    throw 'buf.writeInt16LE error'
  }
  print(`node-polyfill_writeInt16LE: ${endTime - startTime} ms`)
}

function writeInt32BE_Test() {
  const buf = Buffer.allocUnsafe(4);
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    buf.writeInt32BE(0x01020304, 0);
  }
  let endTime = Date.now();
  if (buf.length != 4) {
    throw 'buf.writeInt32BE error'
  }
  print(`node-polyfill_writeInt32BE: ${endTime - startTime} ms`)
}

function writeInt32LE_Test() {
  const buf = Buffer.allocUnsafe(4);
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    buf.writeInt32LE(0x05060708, 0);
  }
  let endTime = Date.now();
  if (buf.length != 4) {
    throw 'buf.writeInt32LE error'
  }
  print(`node-polyfill_writeInt32LE: ${endTime - startTime} ms`)
}

function writeIntBE_Test() {
  const buf = Buffer.allocUnsafe(6);
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    buf.writeIntBE(0x1234567890ab, 0);
  }
  let endTime = Date.now();
  if (buf.length != 6) {
    throw 'buf.writeIntBE error'
  }
  print(`node-polyfill_writeIntBE: ${endTime - startTime} ms`)
}

function writeIntLE_Test() {
  const buf = Buffer.allocUnsafe(6);
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    buf.writeIntLE(0x1234567890ab, 0);
  }
  let endTime = Date.now();
  if (buf.length != 6) {
    throw 'buf.writeIntLE error'
  }
  print(`node-polyfill_writeIntLE: ${endTime - startTime} ms`)
}

function writeUInt8_Test() {
  const buf = Buffer.allocUnsafe(4);
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    buf.writeUInt8(0x42, 0);
  }
  let endTime = Date.now();
  if (buf.length != 4) {
    throw 'buf.writeUInt8 error'
  }
  print(`node-polyfill_writeUInt8: ${endTime - startTime} ms`)
}

function writeUInt16BE_Test() {
  const buf = Buffer.allocUnsafe(4);
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    buf.writeUInt16BE(0xdead, 0);
  }
  let endTime = Date.now();
  if (buf.length != 4) {
    throw 'buf.writeUInt16BE error'
  }
  print(`node-polyfill_writeUInt16BE: ${endTime - startTime} ms`)
}

function writeUInt16LE_Test() {
  const buf = Buffer.allocUnsafe(4);
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    buf.writeUInt16LE(0xdead, 0);
  }
  let endTime = Date.now();
  if (buf.length != 4) {
    throw 'buf.writeUInt16LE error'
  }
  print(`node-polyfill_writeUInt16LE: ${endTime - startTime} ms`)
}

function writeUInt32BE_Test() {
  const buf = Buffer.allocUnsafe(4);
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    buf.writeUInt32BE(0xfeedface, 0);
  }
  let endTime = Date.now();
  if (buf.length != 4) {
    throw 'buf.writeUInt32BE error'
  }
  print(`node-polyfill_writeUInt32BE: ${endTime - startTime} ms`)
}

function writeUInt32LE_Test() {
  const buf = Buffer.allocUnsafe(4);
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    buf.writeUInt32LE(0xfeedface, 0);
  }
  let endTime = Date.now();
  if (buf.length != 4) {
    throw 'buf.writeUInt32LE error'
  }
  print(`node-polyfill_writeUInt32LE: ${endTime - startTime} ms`)
}

function writeUIntBE_Test() {
  const buf = Buffer.allocUnsafe(6);
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    buf.writeUIntBE(0x1234567890ab, 0, 6);
  }
  let endTime = Date.now();
  if (buf.length != 6) {
    throw 'buf.writeUIntBE error'
  }
  print(`node-polyfill_writeUIntBE: ${endTime - startTime} ms`)
}

function writeUIntLE_Test() {
  const buf = Buffer.allocUnsafe(6);
  let startTime = Date.now();
  for (let i = 0; i < 200000; i++) {
    buf.writeUIntLE(0x1234567890ab, 0, 6);
  }
  let endTime = Date.now();
  if (buf.length != 6) {
    throw 'buf.writeUIntLE error'
  }
  print(`node-polyfill_writeUIntLE: ${endTime - startTime} ms`)
}

function buffer_Test() {
  alloc_Test_01();
  alloc_Test_02();
  alloc_Test_03();
  alloc_Test_04();
  alloc_Test_05();
  alloc_Test_06();
  allocUnsafe_Test();
  allocUnsafeSlow_Test();
  byteLength_Test();
  concat_Test();
  from_Test_01();
  from_Test_02();
  from_Test_03();
  from_Test_04();
  from_Test_05();
  from_Test_06();
  isBuffer_Test();
  isEncoding_Test();
  compare_Test();
  copy_Test();
  entries_Test();
  equals_Test();
  fill_Test_01();
  fill_Test_02();
  fill_Test_03();
  includes_Test_01();
  includes_Test_02();
  includes_Test_03();
  indexOf_Test_01();
  indexOf_Test_02();
  indexOf_Test_03();
  lastIndexOf_Test_01();
  lastIndexOf_Test_02();
  lastIndexOf_Test_03();
  subarray_Test();
  swap16_Test();
  swap32_Test();
  swap64_Test();
  toString_Test_01();
  toString_Test_02();
  toString_Test_03();
  keys_Test();
  values_Test();
  readBigInt64BE_Test();
  readBigInt64LE_Test();
  readBigUInt64BE_Test();
  readBigUInt64LE_Test();
  readDoubleBE_Test();
  readDoubleLE_Test();
  readFloatBE_Test();
  readFloatLE_Test();
  readInt8_Test();
  readInt16BE_Test();
  readInt16LE_Test();
  readInt32BE_Test();
  readInt32LE_Test();
  readIntBE_Test();
  readIntLE_Test();
  readUInt8_Test();
  readUInt16BE_Test();
  readUInt16LE_Test();
  readUInt32BE_Test();
  readUInt32LE_Test();
  readUIntBE_Test();
  readUIntLE_Test();
  write_Test();
  writeBigInt64BE_Test();
  writeBigInt64LE_Test();
  writeBigUInt64BE_Test();
  writeBigUInt64LE_Test();
  writeDoubleBE_Test();
  writeDoubleLE_Test();
  writeFloatBE_Test();
  writeFloatLE_Test();
  writeInt8_Test();
  writeInt16BE_Test();
  writeInt16LE_Test();
  writeInt32BE_Test();
  writeInt32LE_Test();
  writeIntBE_Test();
  writeIntLE_Test();
  writeUInt8_Test();
  writeUInt16BE_Test();
  writeUInt16LE_Test();
  writeUInt32BE_Test();
  writeUInt32LE_Test();
  writeUIntBE_Test();
  writeUIntLE_Test();
}
buffer_Test();
